import wx
import time
import datetime

from Project.HTools.time.time_panel import TimePanel


class TimePanelMain(TimePanel):
    def __init__(self, parent):
        TimePanel.__init__(self, parent)
        self.__init_view()

    def startTiming(self, event):
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.updateTime, self.timer)
        self.timer.Start(1000)

    def stopTiming(self, event):
        self.timer.Stop()

    def refreshTiming(self, event):
        self.timer.Stop()
        self.timer.Start(1000)

    def updateTime(self, event):
        millis = int(round(time.time() * 1000))
        self.edit_now_ms.SetValue(str(millis))

    def timeStampToDate(self, event):
        try:
            input_value = int(self.m_textCtrl3.GetValue())
            converted_time = datetime.datetime.fromtimestamp(input_value / 1000.0)
            result_date = converted_time.strftime('%Y-%m-%d %H:%M:%S')
            self.edit_date_str.SetValue(result_date)
        except ValueError:
            self.__show_message('输入的时间戳格式错误')

    def __init_view(self):
        self.edit_now_ms.SetForegroundColour(wx.RED)

    def __show_message(self, message):
        wx.MessageDialog(self, message, '操作提醒', wx.OK).ShowModal()
